package com.seven.tools.pattern;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具
 * 常用的正则匹配
 */
public class PatternUtils {

    /**
     * 是否是ip
     * @param ip
     * @return
     */
    public static boolean isIp(String ip) {
        if (ip==null){return false;}
        return ip.matches("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    }



    /**
     * 匹配字符串中第一个ip字符
     *
     * @param ip
     * @return
     */
    public static String getIp(String ip) {
        String regEx = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(ip);
        while (m.find()) {
            return m.group();
        }
        return null;
    }
}
